package com.kobeliu.controller;

import com.kobeliu.api.interceptor.JwtCurrentUserInterceptor;
import com.kobeliu.pojo.Admin;
import com.kobeliu.pojo.bo.AdminInfoBO;
import com.kobeliu.pojo.vo.AdminVO;
import com.kobeliu.result.GraceJSONResult;
import com.kobeliu.result.ResponseStatusEnum;
import com.kobeliu.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/admininfo")
@Slf4j
public class AdminInfoController {

    @Autowired
    private AdminService adminService;

    /**
     * 创建管理员
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,path = "create")
    public GraceJSONResult create(@Valid @RequestBody AdminInfoBO adminInfoBO){

        adminService.createAdmin(adminInfoBO);
        return GraceJSONResult.ok();

    }

    /**
     * 分页查询admin列表
     * @param accountName
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,path = "list")
    public GraceJSONResult list(Integer page,Integer limit,String accountName){

        //赋默认值
        if(page == null) page = 1;
        if(limit == null) limit = 10;

        return GraceJSONResult.ok(adminService.getAdminList(accountName,page,limit));
    }

    /**
     * 删除admin账号
     * @param username
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,path = "delete")
    public GraceJSONResult delete(String username){

        adminService.deleteAdmin(username);

        return GraceJSONResult.ok();
    }
}
